import moment from "moment"

export default class DataAdapter {
    set data(d) {
        this._d = d
        this.parseData()
    }

    get data() {
        return this._d || []
    }

    get options() {
        let arr1 = []
        for (const key in this._constraint) {
            let m = this._constraint[key]
            if (m && !m.parent) {
                arr1.push(key)
            }
        }

        let arr2 = []
        arr1.forEach(k => {
            arr2.push(k)
            let a3 = this.computeOpendKeys(k)
            arr2 = arr2.concat(a3)
        })

        return arr2
    }

    get timeZone() {
        const cache = this._constraint
        let startTime = moment("2500-01-01"), endTime = moment("1970-01-01")
        for (const key in cache) {
            let plans = cache[key].data.plans || []
            plans.forEach(p => {
                const { start, end } = p
                if (moment(start).isBefore(startTime)) {
                    startTime = moment(start)
                }

                if (moment(endTime).isBefore(moment(end))) {
                    endTime = moment(end)
                }

            })
        }

        let deadTime = moment(endTime).format("YYYY-MM-DD")

        startTime = startTime.add(-1, "months").startOf("month")
        endTime = endTime.add(1, "months").endOf("month")
        return {
            startTime: moment(startTime).format("YYYY-MM-DD"),
            endTime: moment(endTime).format("YYYY-MM-DD"),
            deadTime: deadTime
        }
    }


    parseData() {
        this.resetVals()
        let d = this.data
        this.parseConstraint(d)
    }

    resetVals() {
        this._constraint = {}
        this._spread = {}
    }

    parseConstraint(datas, parent, level = 1) {
        if (!Array.isArray(datas) || datas.length < 1) return
        datas.forEach(d => {
            let key = d.id
            let canExpand = Array.isArray(d.children) && d.children.length > 0
            this._constraint[key] = {
                key,
                parent,
                data: d,
                level,
                canExpand,
                showFlag: "open"
            }
            this.parseConstraint(d.children, key, level + 1)
        })

    }


    closeKey(pk) {
        delete this._spread[pk]
        this._constraint[pk].showFlag = "open"
    }

    openKey(pk) {
        let cache = this._constraint
        let children = []
        for (const subKey in cache) {
            if (subKey === pk) {
                continue
            }
            let f = this.belondsTo(subKey, pk, 1)
            if (f) {
                children.push(subKey)
            }
        }
        if (children.length > 0) {
            this._spread[pk] = children
            this._constraint[pk].showFlag = "close"
        }
        return children
    }

    computeOpendKeys(pk) {
        let cache = this._spread
        let children = cache[pk]
        if (!Array.isArray(children) || children.length < 1) return []

        let arr = []
        children.forEach(k => {
            arr.push(k)
            let a2 = this.computeOpendKeys(k)
            arr = arr.concat(a2)
        })

        return arr
    }

    getDisplayCells(keys) {
        let cache = this._constraint
        let arr = []
        keys.forEach(k => {
            arr.push(cache[k])
        })

        return arr
    }

    getPlans(keys) {
        const constraint = this._constraint
        const spread = this._spread
        let results = []
        keys.forEach(k => {
            let rawData = constraint[k].data || {}
            let plans = rawData.plans || []
            if (!spread[k]) {
                //未展开
                let dps = this.getDescendantPlans(rawData.children)
                plans = plans.concat(dps)
            }
            let ob = {
                ...rawData,
                key: k,
                plans
            }
            results.push(ob)
        })

        return results
    }

    getDescendantPlans(datas) {
        if (!Array.isArray(datas) || datas.length < 1) return []
        let plans = []
        datas.forEach(d => {
            let p1 = d.plans || []
            let p2 = this.getDescendantPlans(d.children)
            plans = plans.concat(p1).concat(p2)
        })
        return plans
    }

    belondsTo(subKey, parentKey, level) {
        let cache = this._constraint
        if (!cache[subKey]) return false
        if (subKey === parentKey) return true
        if (level === 0) return false
        let parent = cache[subKey].parent
        if (!parent) return false
        if (parent === parentKey) return true
        return this.belondsTo(parent, parentKey, level - 1)
    }

}